package cn.message.web;

import cn.itcast.pojo.classsify;
import cn.itcast.pojo.helpCenter;
import cn.itcast.util.OutputStringController;
import cn.message.service.IHelpCenterService;
import cn.web.web_module.util.IconPath;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.OctetStreamData;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author 郭智超
 * @create 2020-05-13 14:58
 */
@Slf4j
@RestController
@RequestMapping(value="/dhn/message",produces="text/html;charset=UTF-8")
public class HelpCenterController {

    @Resource
    OutputStringController osc;

    @Resource
    private IHelpCenterService helpCenterService;

    @ResponseBody
    @RequestMapping("/getHelpById")
    public String getHelpById(@RequestBody Map<String,String> map){
        String results = "";
        if (map.get("id").isEmpty()){
            return osc.failure("请选择一个问题");
        }
        helpCenter help = helpCenterService.getHelpById(map);
        if (help == null){
            return osc.failure("没有该问题");
        }
        results = osc.resultSuccess("查询成功",help);
        return results;
    }

    @ResponseBody
    @RequestMapping("/getAllHelp")
    public String getAllHelp(@RequestBody Map<String,String> map){
        String results = "";
        Map<String, Object> resultMap = helpCenterService.getAllHelp(map);
        //获取帮助中心所有的问题
        Object helpCenterList = resultMap.get("helpCenterList");
        //获取总记录数
        int count = (int)resultMap.get("count");
        if(helpCenterList == null){
            results = osc.resultFailure("查询所有帮助中心信息失败", "");
            log.debug("查询所有帮助中心信息失败");
        }
        else {
            results = osc.resultSuccess("查询所有帮助中心信息成功",helpCenterList, count);
            log.debug("查询所有帮助中心信息成功");
        }
        return results;
    }

    @ResponseBody
    @RequestMapping("/addHelp")
    public String addHelp(@RequestBody Map<String,String> map){
        String results = "";
        log.debug(map.get("question"));
        if (map.get("question").isEmpty()){
            return osc.failure("问题不能为空");
        }
        if (map.get("solution").isEmpty()){
            return osc.failure("答案不能为空");
        }
        Boolean aBoolean = helpCenterService.addHelp(map);
        if (aBoolean){
            results = osc.success("添加成功");
        }else {
            results = osc.failure("添加是吧");
        }
        return results;
    }

    @ResponseBody
    @RequestMapping("/updateHelp")
    public String updateHelp(@RequestBody Map<String,String> map){
        String results = "";
        Boolean aBoolean = helpCenterService.updateHelp(map);
        if(aBoolean){
            results = osc.success("修改成功");
        }else {
            results = osc.failure("修改失败");
        }
        return results;
    }

    @ResponseBody
    @RequestMapping("/deleteHelp")
    public String deleteHelp(@RequestBody Map<String,String> map){
        String results = "";
        Boolean aBoolean = helpCenterService.deleteHelp(map);
        if(aBoolean){
            results = osc.success("删除成功");
        }else {
            results = osc.failure("删除失败");
        }
        return results;
    }

    @ResponseBody
    @PostMapping(value = "/uploadImage",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateIcon(HttpServletRequest request,
                             @RequestParam(value = "img",required = false) MultipartFile img){
        log.debug("开始上传图片了-----------------------------------------------------------");
        String results="";
        if(img.isEmpty()){
            return osc.failure("图片为空，请上传图片");
        }

        //设置文件存储路径
//        String iconPath = "/usr/tomcat/images/dhn/icon/helpCenter/";//送注释
        String iconPath = IconPath.ICONPATH +"icon/helpCenter/";
//        String iconPathEnum = "E:/job/testIconPath/";
        File files = new File(iconPath);
        if(!files.exists() && !files.isDirectory()) {
            System.out.println("目录不存在，创建目录："+files);
            //如果目录不存在则创建目录
            files.mkdir();
        }
        //获取原始文件名称（包含格式）
        String originalIconName = img.getOriginalFilename();
        System.out.println("原始图片名称："+originalIconName);

        //获取文件类型，以最后一个'.'为标识
        String iconType = originalIconName.substring(originalIconName.lastIndexOf(".")+1);
        System.out.println("图片类型："+iconType);

        //获取文件名称（不含格式）
        String iconName = originalIconName.substring(0,originalIconName.lastIndexOf("."));

        //设置文件新名称
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate = sdf.format(new Date());
        //设置新文件名称（包含格式）
        iconName = newDate+iconName+"."+iconType;
        System.out.println("新文件名称"+iconName);

        //在指定路径下创建文件
        File targetFile = new File(iconPath,iconName);

        //将图片保存到指定位置
        try {
            img.transferTo(targetFile);

            //图片在服务器中的地址
//            results = osc.resultSuccess("上传成功","http://120.79.51.10:8181/dhn/icon/helpCenter/"+iconName);
            results = osc.resultSuccess("上传成功","https://www.dhnep.cn:8181/dhnep/icon/helpCenter/"+iconName);

            //图片在本地的地址
//            results = osc.resultSuccess("上传成功",iconPathEnum+iconName);
            System.out.println("上传成功");
        } catch (IOException e) {
            System.out.println("上传失败");
            e.printStackTrace();
            return osc.failure("上传图片失败");
        }

        return results;
    }
}
